package com.fc.service.impl;

import org.hibernate.Criteria
import org.hibernate.criterion.Restrictions
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

import com.fc.entities.User
import com.fc.entities.Workout
import com.fc.service.WorkoutService
import com.fc.service.exceptions.FCException

@Repository("workoutService")
@Transactional
public class WorkoutServiceImpl extends BaseServiceImpl<Workout> implements WorkoutService{
	Workout findByName(String name) {
		Criteria criteria = currentSession().createCriteria(Workout.class);
		Workout entity = (Workout) criteria.add(Restrictions.eq("name", name)).uniqueResult();
		return entity;
	}

	@Override
	void delete(Workout workout) {
		try {
			super.delete(workout)
		} catch (Exception ex) {
			throw new FCException("Az adott edzés (${workout.name}) törlése nem lehetséges.")
		}
	}

	Workout create(Workout workout) {
		try {
			super.create(workout)
		} catch (Exception ex) {
			throw new FCException("Az adott edzés (${workout.name}) létrehozása nem lehetséges.")
		}
	}

	List<Workout> findAll() {
		findAll(Workout.class)
	}

	List<Workout> findAllByUser(User user) {
		Criteria criteria = currentSession().createCriteria(Workout.class)
		criteria.add(Restrictions.eq("user", user))
		return criteria.list()
	}
}
